package cn.ekuma.epos.swing.panel;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JToolBar;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.table.AbstractTableModel;
import javax.swing.JButton;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;


public class JHashTableEditor extends JPanel {
	private JTable table;
	private HashMap props=new HashMap();;
	private ArrayList keys;
	PropTableModel model;
	private JButton btnNewButton;
	private JButton btnNewButton_1;

	/**
	 * Create the panel.
	 */
	public JHashTableEditor() {
		initComponents();
		model=new PropTableModel();
		table.setModel(model);
	}
	
	private void initComponents() {
		setLayout(new BorderLayout(0, 0));
		
		JToolBar toolBar = new JToolBar();
		add(toolBar, BorderLayout.NORTH);
		
		 btnNewButton = new JButton("+");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addProp();
			}
		});
		
		jPropText = new JTextField();
		toolBar.add(jPropText);
		jPropText.setColumns(10);
		btnNewButton.setMaximumSize(new Dimension(45, 23));
		btnNewButton.setPreferredSize(new Dimension(45, 23));
		toolBar.add(btnNewButton);
		
		btnNewButton_1 = new JButton("-");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				removeProp();
			}
		});
		btnNewButton_1.setPreferredSize(new Dimension(45, 23));
		btnNewButton_1.setMaximumSize(new Dimension(45, 23));
		toolBar.add(btnNewButton_1);
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, BorderLayout.CENTER);
		
		table = new JTable();
		scrollPane.setViewportView(table);
	}

	
	protected void removeProp() {
		int selected=table.getSelectedRow();
		if(selected>=0){
			props.remove(keys.get(selected));
			keys.remove(selected);
			model.fireTableStructureChanged();
		}
	}

	protected void addProp() {
		String propTxt=jPropText.getText();
		if(propTxt==null||propTxt.isEmpty())
			return;
		keys.add(propTxt);
		props.put(propTxt, null);
		model.fireTableStructureChanged();
	}

	public void setHashMap(HashMap<String,String> props1,boolean addAble){
		this.props=props1;
		if(props==null)
			this.props=new HashMap();
		keys=new ArrayList<String>();
		Iterator<String> a=props.keySet().iterator();
		while(a.hasNext())
			keys.add(a.next());
		btnNewButton.setEnabled(addAble);
		btnNewButton_1.setEnabled(addAble);
		jPropText.setEditable(addAble);
	}
	
	public HashMap getHashMap(){
		return props;
	}

	private final static String[] SALEHEADERS = {"key", "value"};
	private JTextField jPropText;
	
	 class PropTableModel extends AbstractTableModel{
		  
		public PropTableModel() {
		}
		 
		public String getColumnName(int column) {
	        return SALEHEADERS[column];
	    }
	    
		public int getRowCount() {
	        return props.size();
	    }
		 
	    public int getColumnCount() {
	        return SALEHEADERS.length;
	    }
	    
	    public Object getValueAt(int row, int column) {
	        switch (column) {
	        case 0: return keys.get(row);
	        case 1: return props.get(keys.get(row));
	        default: return null;
	        }
	    }

		@Override
		public boolean isCellEditable(int rowIndex, int columnIndex) {
			if(columnIndex==1)
				return true;
			return super.isCellEditable(rowIndex, columnIndex);
		}

		@Override
		public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
			props.put(keys.get(rowIndex),(String) aValue);
		}
	}
	
	
}
